package com.imooc.course.business.controller.album;

import com.imooc.course.business.controller.admin.CourseController;
import com.imooc.course.business.form.CoursePageForm;
import com.imooc.course.server.dto.CourseDto;
import com.imooc.course.server.dto.CoursePageDto;
import com.imooc.course.server.dto.ResponseDto;
import com.imooc.course.server.enums.CourseStatusEnum;
import com.imooc.course.server.service.CourseService;
import com.imooc.course.server.utils.CopyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/album/course")
public class AlbumCourseController {

    private static final Logger log = LoggerFactory.getLogger(CourseController.class);
    public static final String BUSINESS_NAME = "Album Course";


    @Resource(name = "courseService")
    private CourseService courseService;

    private void setCourseStatus(CoursePageForm pageForm) {
        pageForm.setStatus(CourseStatusEnum.PUBLISH.getCode());
    }

    /**
     * 查询最新已发布的课程列表
     * @return ResponseDto
     */
    @GetMapping("/new/courses")
    public ResponseDto getList() {
        CoursePageForm pageForm = new CoursePageForm();
        this.setCourseStatus(pageForm);
        pageForm.setPage(1);
        pageForm.setSize(3);
        CoursePageDto coursePageDto = CopyUtil.copy(pageForm, CoursePageDto.class);
        courseService.getNewlyReleasedCourseList(coursePageDto);
        return ResponseDto.success().setContent(coursePageDto.getList());
    }

    /**
     * 查询最报名数最多的课程列表
     * @return ResponseDto
     */
    @PostMapping("/hot/courses")
    public ResponseDto getHotCourses(@RequestBody CoursePageForm pageForm) {
        this.setCourseStatus(pageForm);
        CoursePageDto coursePageDto = CopyUtil.copy(pageForm, CoursePageDto.class);
        courseService.getHotCourses(coursePageDto);
        return ResponseDto.success().setContent(coursePageDto);
    }

    /**
     * 查询全部课程
     * @param pageForm 传入的page和size
     * @return
     */
    @PostMapping("/all/courses")
    public ResponseDto getAllCourses(@RequestBody CoursePageForm pageForm) {
        this.setCourseStatus(pageForm);
        CoursePageDto pageDto = CopyUtil.copy(pageForm, CoursePageDto.class);
        courseService.getList(pageDto);
        return ResponseDto.success().setContent(pageDto);
    }

    /**
     * 查询全部课程
     * @param pageForm 传入的page和size
     * @return
     */
    @PostMapping("/select/courses")
    public ResponseDto getSelectCourses(@RequestBody CoursePageForm pageForm) {
        if (StringUtils.isEmpty(pageForm.getCategoryId())) {
            return this.getAllCourses(pageForm);
        }
        this.setCourseStatus(pageForm);
        CoursePageDto pageDto = CopyUtil.copy(pageForm, CoursePageDto.class);
        courseService.searchCoursesByCategoryId(pageDto);
        return ResponseDto.success().setContent(pageDto);
    }




    /**
     * 通过id查询课程及课程相关的，内容描述，讲师，大章，小节，分类
     * @return
     */
    @PostMapping("/course/detail")
    public ResponseDto getCourseDetailByCourseId(String courseId) {
        CourseDto courseDto = courseService.getCourseDetailByCourseId(courseId);
        return ResponseDto.success().setContent(courseDto);
    }

}
